<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>公告列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script src="__ADMIN__/lib/layui/layui.js" charset="utf-8"></script>
    <link rel="stylesheet" href="__ADMIN__/css/font.css">
    <link rel="stylesheet" href="__ADMIN__/css/xadmin.css">
    <script type="text/javascript" src="__ADMIN__/js/xadmin.js"></script>
</head>
<body>
<table id="test" lay-filter="test" lay-data="{id: 'test'}"></table>

<!--//头部工具栏-->
<script type="text/html" id="toolbarDemo">
    <div>
        <div class="demoTable">
            <div class="layui-form">
                搜索类型
                <div class="layui-input-inline">
                    <select id="check" name="check" class="layui-select" lay-verify="required"
                            lay-filter="selectTemplate" lay-search width="80px">
                        <option value=""></option>
                        <option value="1">ID</option>
                        <option value="2">内容</option>
                        <option value="3">时间搜索</option>
                    </select>
                </div>
                <div class="layui-input-inline layui-show-xs-block">
                    <input class="layui-input" placeholder="开始时间" name="start" id="start"></div>
                <div class="layui-input-inline layui-show-xs-block">
                    <input class="layui-input" placeholder="截止时间" name="end" id="end"></div>
                搜索内容：
                <div class="layui-inline">
                    <input class="layui-input" name="value" id="inputval" autocomplete="off" required>
                </div>
                <button type="button" class="layui-btn" data-type="reload">搜索</button>
            </div>
        </div>
    </div>

    <div class="layui-btn-container">
        <button type="button" class="layui-btn layui-btn-sm" lay-event="addData">新增 <span
                class="glyphicon glyphicon-plus"></span></button>
        <button class="layui-btn layui-btn-sm" lay-event="refresh">刷新表格</button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="dels">删除选中行</button>
    </div>
</script>

<!--//操作栏-->
<script type="text/html" id="barDemo">
    <a style=" text-decoration: none"><i class="layui-icon layui-icon-edit toolbars" lay-event="edit" title="编辑"></i>
    </a>
    <a style=" text-decoration: none"><i class="layui-icon layui-icon-delete" lay-event="del" title="删除"></i> </a>
</script>

<!--    编辑弹出层-->
<script type="text/html" id="edit_form">
    <div class="layui-row">
        <div class="layui-form">
            <div class="text" style="width: 1000px;height: 100%;margin: auto;padding-top: 20px;">
                <div class="layui-form-item" style="width: 45%;float: left;clear: none;margin-bottom: 30px;">
                    <label class="layui-form-label">
                        <span class="x-red">*</span>ID</label>
                    <div class="layui-input-inline">
                        <input type="text" id="id" name="id" required="" lay-verify="required|number|idcheck"
                               autocomplete="off"
                               class="layui-input"></div>
                    <div class="layui-form-mid layui-word-aux">
                        <span class="x-red">*</span>将会成为公告唯一标识
                    </div>
                </div>

                <div class="layui-form-item" style="width: 45%;float: left;clear: none;margin-bottom: 30px;">
                    <label class="layui-form-label">
                        <span class="x-red">*</span>发布时间</label>
                    <div class="layui-input-inline">
                        <input type=type="text" id="time" name="time" required autocomplete="off"
                               class="layui-input"></div>
                    <div class="layui-form-mid layui-word-aux">
                    </div>
                </div>

                <hr class="hr20">
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="x-red">*</span>公告内容</label>
                    <div class="layui-input-inline">
                    <textarea rows="10" cols="30" class="tarea" id="content" name="content" required=""
                              lay-verify="required"
                              autocomplete="off" style="width: 800px; height: 200px;margin: 0;"></textarea>
                    </div>

                    <hr class="hr20">
                    <div class="layui-form-item">
                        <label class="layui-form-label"></label>
                        <button class="layui-btn" lay-filter="update" lay-submit="update">修改</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</script>

</body>
<script>
    var editid;
    var uneditid;

    //初始化日期控件 以及日期选择限制问题
    layui.use('laydate', function () {
        var laydate = layui.laydate;
        var endDate = laydate.render({
            elem: '#end',//选择器结束时间
            type: 'datetime',
            min: "1970-1-1",//设置min默认最小值
            done: function (value, date) {
                startDate.config.max = {
                    year: date.year,
                    month: date.month - 1,//关键
                    date: date.date,
                    hours: 0,
                    minutes: 0,
                    seconds: 0
                }
            }
        });
        //日期范围
        var startDate = laydate.render({
            elem: '#start',
            type: 'datetime',
            max: "2099-12-31",//设置一个默认最大值
            done: function (value, date) {
                endDate.config.min = {
                    year: date.year,
                    month: date.month - 1, //关键
                    date: date.date,
                    hours: 0,
                    minutes: 0,
                    seconds: 0
                };
            }
        });

    });

    layui.use(['form', 'layer', 'jquery', 'table'], function () {
        var table = layui.table;
        $ = layui.jquery;
        var form = layui.form,
            layer = layui.layer;

        //修改监听
        form.on('submit(update)', function (data) {
            var id = data.field.id;
            var content = data.field.content;
            var time = data.field.time;
           // console.log(data.field);
            editid = id;
            if (editid == uneditid) {
                $.ajax({
                    type: "post",
                    url: "../Admin/noticeupdate",
                    dataType: "json",
                    data: {
                        "id": id, "content": content, "time": time
                    },
                    async: false,//必须是同步提交的
                    success: function (data) {
                      //  console.log(data);
                        layer.alert('修改成功', {icon: 6}, function (index) {
                            layer.closeAll();
                            table.reload('searchreload');
                        });
                    },
                });
            } else {
                $.ajax({
                    type: "post",
                    url: "{:url('Admin/noticedel')}",
                    dataType: "json",
                    data: {"id": uneditid},
                    async: false,
                    success: function (data) {
                        $.ajax({
                            url: "../Admin/noticeidcheck",
                            type: 'POST',
                            dataType: "json",
                            data: {"id": editid},
                            async: false,//必须是同步提交的
                            success: function (res) {
                                // console.log(res.data);
                                if (res.data == 1) {
                                    layer.msg('ID已存在，请修改ID！', {icon: 5});
                                } else if (res.data == 0) {
                                    $.ajax({
                                        url: "../Admin/noticeinsert",
                                        type: "post",
                                        dataType: "json",
                                        data: {
                                            "id": id, "content": content, "time": time
                                        },
                                        async: false,//必须是同步提交的
                                        success: function (t) {
                                         //   console.log(t);
                                            layer.alert('修改成功', {icon: 6}, function (index) {
                                                layer.closeAll();
                                                table.reload('searchreload');

                                            });
                                        },
                                    });

                                }
                            },
                        });
                    }
                });
            }
        });
    });
    layui.use('table', function () {
        var table = layui.table;
        table.render({
            even: false //开启隔行背景
         //   , size: 'sm' //小尺寸的表格
            , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , minheight: "800px"
            , elem: '#test'
            , page: true
            , id: 'searchreload'
            , limit: 12
            , height: 650
            , url: "{:url('Admin/noticejson')}"
            , parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": 0, //解析接口状态
                    "msg": "刷新页面试试", //解析提示文本
                    "count": res.total, //解析数据长度
                    "data": res.data//解析数据列表
                };
            },
            cols: [[
                {field: 'id', type: 'checkbox', width: 50, fixed: 'left'}
                , {field: 'id', title: '公告编号', width: 150, sort: true}
                , {field: 'content', title: '公告内容', width: 800}
                , {field: 'time', title: '发布时间', width: 150, sort: true}
                , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 120}
            ]],

        });

        //查询表格重载  两种的查询方式

        $('.demoTable .layui-btn').on('click', function () {
            table.reload('searchreload', {
                where: { //设定异步数据接口的额外参数，任意设
                    value: $("#inputval").val(),
                    type: document.getElementById("check").value,
                    start: $("#start").val(),
                    end: $("#end").val()
                },
                page: {
                    curr: 1
                }
            });
        });


        //监听头部工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                //新增
                case 'addData':
                    layer.open({
                        type: 2,
                        title: '新的公告',
                        shadeClose: true,
                        shade: false,
                        //btn: ['确定', '取消'],
                        maxmin: true, //开启最大化最小化按钮
                        area: ['1100px', '540px'],
                        content: ['noticeadd'],
                        end: function () {
                            table.reload('searchreload');
                        }
                    });
                    break;


                //批量删除
                case 'dels':
                    var data = checkStatus.data;
                    var ids = [];
                    data.forEach(function (item) {
                        ids.push(item.id);
                    });
                    // console.log(ids);
                    if (data.length == '') {
                        layer.msg("未选择数据");
                    } else {
                        layer.confirm('真的删除选中数据吗', function (index) {
                            layer.close(index);
                            $.ajax({
                                type: "post",
                                url: "{:url('Admin/noticedels')}",
                                dataType: "json",
                                data: {"ids": ids},
                                async: false,
                                success: function (data) {
                                    layer.msg("删除成功");
                                    table.reload('searchreload');
                                }
                            });
                            layer.msg("删除成功");
                            table.reload('searchreload');
                        })
                    }
                    break;
                case 'refresh':
                    location.reload();
                    //able.reload('searchreload');  正确 搜索存在问题 导致无法刷新
                    break;
            }
            ;
        });

        //监听行工具事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    layer.close(index);
                    $.ajax({
                        type: "post",
                        url: "{:url('Admin/noticedel')}",
                        dataType: "json",
                        data: {"id": data.id},
                        async: false,
                        success: function (data) {
                            layer.msg("删除成功");
                            table.reload('searchreload');
                        }
                    });
                });
            } else if (obj.event === 'edit') {
                var id = data.id;
                layui.use('layer', function () {
                    var layer = layui.layer;
                    layer.open({
                        type: 1,
                        title: "公告修改",
                        shade: false,
                        area: ['1100px', '550px'],
                        content: $('#edit_form').html(),
                        success: function (layero, index) {
                            uneditid = id;
                            $.ajax({
                                type: "post",
                                url: "{:url('Admin/geteditnotice')}",
                                dataType: "json",
                                data: {"id": data.id},
                                async: false,
                                success: function (data) {
                                    //选择时间控件
                                    layui.use(['laydate'], function () {
                                        var laydate = layui.laydate;
                                        laydate.render({     //创建时间选择框
                                            elem: '#time' //指定元素
                                            , type: 'datetime' //日期格式

                                        });
                                        // var endTime = $('#time').val();
                                        // console.log(endTime);
                                    });
                                    table.reload('searchreload');
                                    $(" input[name='id']").val(data.list.id);
                                    $(" input[name='time']").val(data.list.time);
                                    $(" textarea[name='content']").val(data.list.content);
                                }
                            });
                        }
                    });
                })
            }
        })
    });
</script>

</html>